<!DOCTYPE html>
<html>

<head>
<title>代理权限设置</title>
<meta http-equiv="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit">

<%include("/common/include.html"){}%>
</head>

<body class="easyui-layout">
	<div data-options="region:'west',split: true" style="width: 20%">
		<div style="padding: 5px;">
			<ul id="levelTree" class="ztree"></ul>
		</div>
	</div>
	<div data-options="region:'center',border: false">
		<div style="padding: 5px;">
			<ul id="rightTree" class="ztree"></ul>
		</div>
	</div>

	<script type="text/javascript">
		var levelTreeSetting = {
			async : {
				enable : true,
				url : '${cxt!}/organLevel/levelTreeData',
				autoParam : [ "id=ids" ],//, "name=n", "level=lv"
				//otherParam : {"otherParam" : "zTreeAsyncTest"},
				type : "post"
			},
			view : {
				expandSpeed : "",
				selectedMulti : false
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			callback : {
				onClick : levelTreeOnClick,
				beforeAsync : beforeAsync,
				onAsyncSuccess : onAsyncSuccess,
				onAsyncError : onAsyncError
			}
		};

		function levelTreeOnClick(event, treeId, treeNode) {
			//重新初始化rightTree
			initRightTree(treeNode);
		}
		
		function initRightTree(treeNode){
			firstAsyncSuccessFlag = 0;
			var curOrganIds = "${curLevelIds! }";
			var ck = true;
			if(treeNode.id == curOrganIds && '4e556d3509c74252aee7424c6b9f6e52' != curOrganIds){
				ck = false;
			}
			var rightTreeSetting = {
				async : {
					enable : true,
					url : '${cxt!}/organLevel/rightTreeData',
					autoParam : [ "id=ids" ],//, "name=n", "level=lv"
					otherParam : {
						"levelIds" : treeNode.id
					},
					type : "post"
				},
				view : {
					fontCss : getFont,
					expandSpeed : "",
					selectedMulti : false
				},
				data : {
					simpleData : {
						enable : true
					}
				},
				check : {
					enable : ck,
					chkStyle : "checkbox",
					chkboxType : {
						"Y" : "ps",
						"N" : "ps"
					}
				},
				callback : {
					beforeCheck : rightBeforeCheck,
					onAsyncSuccess : rightTreeOnAsyncSuccess
				}
			};
			$.fn.zTree.init($("#rightTree"), rightTreeSetting);
		}

		//rightTree加载完成后默认展开第一级
		var firstAsyncSuccessFlag = 0;
		function rightTreeOnAsyncSuccess(event, treeId, msg) {
			if (firstAsyncSuccessFlag == 0) {
				try {
					var rightTree = $.fn.zTree.getZTreeObj("rightTree");
					//调用默认展开第一个结点  
					var nodes = rightTree.getNodes();
					rightTree.expandNode(nodes[0], true);

					var childNodes = rightTree.transformToArray(nodes[0]);
					rightTree.expandNode(childNodes[1], true);
					firstAsyncSuccessFlag = 1;
				} catch (err) {

				}

			}
		}

		function rightBeforeCheck(treeId, treeNode) {
			var lTree = $.fn.zTree.getZTreeObj("levelTree");
			var curLevelNode = lTree.getSelectedNodes()[0];
			var url = '';
			var msg = '';
			if (treeNode.checked == false) {
				url = "organLevel/addLevelRight";
				msg = "添加成功！";
			} else if (treeNode.checked == true) {
				url = "organLevel/delLevelRight";
				msg = "删除成功！";
			}
			$.post(url, {
				"ids" : treeNode.id,
				"levelIds" : curLevelNode.id
			}, function(result) {
				if (result.code == '200') {
					$.slideMsg(msg, parent.$);
				}else if(result.code == '300'){
					var parentNode = treeNode.getParentNode();
					var rightTree = $.fn.zTree.getZTreeObj("rightTree");
					rightTree.reAsyncChildNodes(parentNode, "refresh");
					parent.$.messager.alert('错误', result.msg, 'error');
				}
			}, "json");
		}

		//字体设置
		function getFont(treeId, node) {
			return node.font ? node.font : {};
		}

		//用于捕获异步加载之前的事件回调函数,zTree 根据返回值确定是否允许进行异步加载
		function beforeAsync() {
			curAsyncCount++;
		}

		//用于捕获异步加载出现异常错误的事件回调函数
		function onAsyncSuccess(event, treeId, treeNode, msg) {
			curAsyncCount--;
			if (curStatus == "expand") {
				expandNodes(treeNode.children);
			} else if (curStatus == "async") {
				asyncNodes(treeNode.children);
			}

			if (curAsyncCount <= 0) {
				if (curStatus != "init" && curStatus != "") {
					asyncForAll = true;
				}
				curStatus = "";
			}
			var levelTree = $.fn.zTree.getZTreeObj("levelTree");
			//获取节点
			var nodes = levelTree.getNodes();
			if (nodes.length > 0) {
				levelTree.selectNode(nodes[0]);
				initRightTree(nodes[0]);
			}
		}
		//用于捕获异步加载正常结束的事件回调函数
		function onAsyncError(event, treeId, treeNode, XMLHttpRequest,
				textStatus, errorThrown) {
			curAsyncCount--;

			if (curAsyncCount <= 0) {
				curStatus = "";
				if (treeNode != null)
					asyncForAll = true;
			}
		}

		var curStatus = "init", curAsyncCount = 0, asyncForAll = false, goAsync = false;

		function expandAll() {
			if (!check()) {
				return;
			}
			var lTree = $.fn.zTree.getZTreeObj("levelTree");
			if (asyncForAll) {
				lTree.expandAll(true);
			} else {
				expandNodes(lTree.getNodes());
				if (!goAsync) {
					curStatus = "";
				}
			}
		}

		function expandNodes(nodes) {
			if (!nodes) {
				return;
			}
			curStatus = "expand";
			var lTree = $.fn.zTree.getZTreeObj("levelTree");
			for (var i = 0, l = nodes.length; i < l; i++) {
				lTree.expandNode(nodes[i], true, false, false);
				if (nodes[i].isParent && nodes[i].zAsync) {
					expandNodes(nodes[i].children);
				} else {
					goAsync = true;
				}
			}
		}

		function asyncAll() {
			if (!check()) {
				return;
			}
			var lTree = $.fn.zTree.getZTreeObj("levelTree");
			if (asyncForAll) {

			} else {
				asyncNodes(lTree.getNodes());
				if (!goAsync) {
					curStatus = "";
				}
			}
		}

		function asyncNodes(nodes) {
			if (!nodes) {
				return;
			}
			curStatus = "async";
			var lTree = $.fn.zTree.getZTreeObj("levelTree");
			for (var i = 0, l = nodes.length; i < l; i++) {
				if (nodes[i].isParent && nodes[i].zAsync) {
					asyncNodes(nodes[i].children);
				} else {
					goAsync = true;
					lTree.reAsyncChildNodes(nodes[i], "refresh", true);
				}
			}
		}

		function check() {
			if (curAsyncCount > 0) {
				return false;
			}
			return true;
		}

		//////////////////初始化////////////////////

		$(document).ready(function() {
			$.fn.zTree.init($("#levelTree"), levelTreeSetting);
		});

		setTimeout(function() {
			expandAll();
		}, 500);
	</script>

</body>
</html>